<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="合同编号" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同名称" prop="contractName">
        <el-input
          v-model="queryParams.contractName"
          placeholder="请输入合同名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select
          v-model="queryParams.orderType"
          placeholder="请选择订单类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.project_order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="签订日期">
        <el-date-picker
          v-model="daterangeSignDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="交付日期">
        <el-date-picker
          v-model="daterangeDeliverDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="是否预付款" prop="isPay">
        <el-input
          v-model="queryParams.isPay"
          placeholder="请输入是否预付款"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.contract_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['contract:ContractInfo:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['contract:ContractInfo:edit']"
          >修改</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['contract:ContractInfo:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['contract:ContractInfo:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="ContractInfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="合同编号" align="center" prop="contractNo" />
      <el-table-column label="合同名称" align="center" prop="contractName" />
      <el-table-column label="订单类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.project_order_type"
            :value="scope.row.orderType"
          />
        </template>
      </el-table-column>
      <el-table-column label="经办人" align="center" prop="operator" />
      <el-table-column label="结构量" align="center" prop="structCount" />
      <el-table-column
        label="签订日期"
        align="center"
        prop="signDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="交付日期"
        align="center"
        prop="deliverDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliverDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="对方单位" align="center" prop="oppositeCompany" />
      <el-table-column label="合同地址" align="center" prop="contractAddress" />
      <el-table-column label="是否预付款" align="center" prop="isPay">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.prepayment_or_not"
            :value="scope.row.isPay"
          />
        </template>
      </el-table-column>
      <el-table-column label="预付金额" align="center" prop="payMount" />
      <el-table-column label="付款方式" align="center" prop="payWay" />
      <el-table-column label="运输方式" align="center" prop="transWay" />
      <el-table-column label="收款方式" align="center" prop="collectWay" />
      <el-table-column label="附件" align="center" prop="filePath" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.contract_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="comment" />
      <el-table-column label="是否发货" align="center" prop="shipmentStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.shipment_status"
            :value="scope.row.shipmentStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['contract:ContractInfo:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="Check(scope.row)"
            v-hasPermi="['contract:ContractInfo:edit']"
            >查看</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['contract:ContractInfo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同信息对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="合同编号" prop="contractNo">
          <el-input v-model="form.contractNo" placeholder="请输入合同编号" />
        </el-form-item>
        <el-form-item label="合同名称" prop="contractName">
          <el-input v-model="form.contractName" placeholder="请输入合同名称" />
        </el-form-item>
        <el-form-item label="订单类型" prop="orderType">
          <el-select v-model="form.orderType" placeholder="请选择订单类型">
            <el-option
              v-for="dict in dict.type.project_order_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="经办人" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入经办人" />
        </el-form-item>
        <el-form-item label="结构量" prop="structCount">
          <el-input v-model="form.structCount" placeholder="请输入结构量" />
        </el-form-item>
        <el-form-item label="签订日期" prop="signDate">
          <el-date-picker
            clearable
            v-model="form.signDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择签订日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="交付日期" prop="deliverDate">
          <el-date-picker
            clearable
            v-model="form.deliverDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择交付日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="对方单位" prop="oppositeCompany">
          <el-input
            v-model="form.oppositeCompany"
            placeholder="请输入对方单位"
          />
        </el-form-item>
        <el-form-item label="合同地址" prop="contractAddress">
          <el-input
            v-model="form.contractAddress"
            placeholder="请输入合同地址"
          />
        </el-form-item>
        <el-form-item label="是否预付款" prop="isPay">
          <el-input v-model="form.isPay" placeholder="请输入是否预付款" />
        </el-form-item>
        <el-form-item label="预付金额" prop="payMount">
          <el-input v-model="form.payMount" placeholder="请输入预付金额" />
        </el-form-item>
        <el-form-item label="付款方式" prop="payWay">
          <el-input v-model="form.payWay" placeholder="请输入付款方式" />
        </el-form-item>
        <el-form-item label="运输方式" prop="transWay">
          <el-input v-model="form.transWay" placeholder="请输入运输方式" />
        </el-form-item>
        <el-form-item label="收款方式" prop="collectWay">
          <el-input v-model="form.collectWay" placeholder="请输入收款方式" />
        </el-form-item>
        <el-form-item label="附件">
          <file-upload v-model="form.filePath" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.contract_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="comment">
          <el-input
            v-model="form.comment"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="是否发货" prop="shipmentStatus">
          <el-select v-model="form.shipmentStatus" placeholder="请选择是否发货">
            <el-option
              v-for="dict in dict.type.shipment_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">合同计划信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddContractPlanInfo"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteContractPlanInfo"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="contractPlanInfoList"
          :row-class-name="rowContractPlanInfoIndex"
          @selection-change="handleContractPlanInfoSelectionChange"
          ref="contractPlanInfo"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="计划名称" prop="planName" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.planName"
                placeholder="请输入计划名称"
              />
            </template>
          </el-table-column>
          <el-table-column label="父计划编号" prop="parentPlanId" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.parentPlanId"
                placeholder="请输入父计划编号"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="计划开始时间"
            prop="planBeginTime"
            width="240"
          >
            <template slot-scope="scope">
              <el-date-picker
                clearable
                v-model="scope.row.planBeginTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择计划开始时间"
              />
            </template>
          </el-table-column>
          <el-table-column label="计划结束时间" prop="planEndTime" width="240">
            <template slot-scope="scope">
              <el-date-picker
                clearable
                v-model="scope.row.planEndTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择计划结束时间"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import {
  listContractInfo,
  getContractInfo,
  delContractInfo,
  addContractInfo,
  updateContractInfo,
} from "@/api/contract/ContractInfo";

export default {
  name: "ContractInfo",
  dicts: ["shipment_status", "project_order_type", "contract_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedContractPlanInfo: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同信息表格数据
      ContractInfoList: [],
      // 合同计划表格数据
      contractPlanInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 合同编号时间范围
      daterangeSignDate: [],
      // 合同编号时间范围
      daterangeDeliverDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractNo: null,
        contractName: null,
        orderType: null,
        signDate: null,
        deliverDate: null,
        isPay: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contractName: [
          { required: true, message: "合同名称不能为空", trigger: "blur" },
        ],
        orderType: [
          { required: true, message: "订单类型不能为空", trigger: "change" },
        ],
        structCount: [
          { required: true, message: "结构量不能为空", trigger: "blur" },
        ],
        oppositeCompany: [
          { required: true, message: "对方单位不能为空", trigger: "blur" },
        ],
        contractAddress: [
          { required: true, message: "合同地址不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询合同信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeSignDate && "" != this.daterangeSignDate) {
        this.queryParams.params["beginSignDate"] = this.daterangeSignDate[0];
        this.queryParams.params["endSignDate"] = this.daterangeSignDate[1];
      }
      if (
        null != this.daterangeDeliverDate &&
        "" != this.daterangeDeliverDate
      ) {
        this.queryParams.params["beginDeliverDate"] =
          this.daterangeDeliverDate[0];
        this.queryParams.params["endDeliverDate"] =
          this.daterangeDeliverDate[1];
      }
      listContractInfo(this.queryParams).then((response) => {
        this.ContractInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractNo: null,
        contractName: null,
        orderType: null,
        operator: null,
        structCount: null,
        signDate: null,
        deliverDate: null,
        oppositeCompany: null,
        contractAddress: null,
        isPay: null,
        payMount: null,
        payWay: null,
        transWay: null,
        collectWay: null,
        filePath: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        status: null,
        comment: null,
        shipmentStatus: null,
      };
      this.contractPlanInfoList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSignDate = [];
      this.daterangeDeliverDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.open = true;
      this.title = "添加合同信息";
      this.$router.push({
        path: "/ContractInfo/ContractInfoNew",
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // const id = row.id || this.ids;
      // getContractInfo(id).then((response) => {
      //   this.form = response.data;
      //   this.contractPlanInfoList = response.data.contractPlanInfoList;
      //   this.open = true;
      //   this.title = "修改合同信息";
      // });
      this.$router.push({
        path: "/ContractInfo/ContractInfoEdit",
        query: { id: row.id },
      });
    },
    // 查看
    Check(row) {
      this.reset();
      this.$router.push({
        path: "/ContractInfo/ContractInfoCheck",
        query: { id: row.id },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.contractPlanInfoList = this.contractPlanInfoList;
          if (this.form.id != null) {
            updateContractInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContractInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除合同信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delContractInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 合同计划序号 */
    rowContractPlanInfoIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 合同计划添加按钮操作 */
    handleAddContractPlanInfo() {
      let obj = {};
      obj.planName = "";
      obj.parentPlanId = "";
      obj.planBeginTime = "";
      obj.planEndTime = "";
      this.contractPlanInfoList.push(obj);
    },
    /** 合同计划删除按钮操作 */
    handleDeleteContractPlanInfo() {
      if (this.checkedContractPlanInfo.length == 0) {
        this.$modal.msgError("请先选择要删除的合同计划数据");
      } else {
        const contractPlanInfoList = this.contractPlanInfoList;
        const checkedContractPlanInfo = this.checkedContractPlanInfo;
        this.contractPlanInfoList = contractPlanInfoList.filter(function (
          item
        ) {
          return checkedContractPlanInfo.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handleContractPlanInfoSelectionChange(selection) {
      this.checkedContractPlanInfo = selection.map((item) => item.index);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "contract/ContractInfo/export",
        {
          ...this.queryParams,
        },
        `ContractInfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
